﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
namespace NASM_Develop_IDE
{
    public partial class BuildDialog : Form
    {
        private Solution solution;
        private StringBuilder output;
        public BuildDialog(Solution solution)
        {
            output = new StringBuilder();
            if (solution == null)
            {
                throw new ArgumentNullException("Argument 'solution' Is Not Pointing To A Valid Object!");
            }
            this.solution = solution;
            InitializeComponent();
            TextBoxHighlighter.Highlight(extraParameters, "Enter Extra Command Line Parameters (Both -f and -o; Including Incorrect Parameters Will Be Ignored)");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        public string BuildingOutput
        {
            get
            {
                return output.ToString();
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            int format = objectFileFormat.SelectedIndex;
            if (format < 0)
            {
                MessageBox.Show("Please Select An Object Format! If Not Sure Please Select -f win32.", "Error",  MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                switch(format)
                {
                    case 0: //WIN32
                    {
                        
                        List<string> sources = solution.AbsolutePathOfSources;
                        int nthObject = 1;
                        sources.ForEach(delegate(string source)
                        {
                            StringBuilder arguments = new StringBuilder("-fwin32");
                            arguments.Append(" ");
                            arguments.Append("\"");
                            arguments.Append(source);
                            arguments.Append("\"");
                            arguments.Append(" ");
                            if (formatSourceName.Checked)
                            {
                                arguments.Append(" -o\"");
                                arguments.Append(solution.AbsolutePathOfBinFolder + "\\" + Path.GetFileNameWithoutExtension(source));
                                arguments.Append(".obj\"");
              
                            }
                            else
                            {
                                arguments.Append(" -o\"");
                                arguments.Append(solution.AbsolutePathOfBinFolder + "\\" + nthObject);
                                arguments.Append(".obj\"");
                            
                            }
                            Process nasmP = null;
                            {

                                    bool successfull = NASMBuilder.Build(arguments.ToString(), out nasmP);
                                    if (successfull) // Successfull in the sense that no exception was thrown while building. Compiler errors and warning might be present if successfully built.
                                    {
                                        string o;
                                        do
                                        {
                                            o = nasmP.StandardOutput.ReadToEnd();
                                            if (o != string.Empty)
                                            {
                                                output.Append(o + "\n\n");
                                            }
                                            else
                                            {
                                                output.Append("Build Successfull!" + "\n\n");
                                            }
                                        }
                                        while (o == string.Empty || o == null);
                                    }
                                    else
                                    {
                                        throw new InvalidOperationException("An Exception Occurred While Building!");
                                    }
                                
                            }
                            if (nasmP != null)
                            {
                                nasmP.Dispose();
                                nasmP = null;
                            }
                            nthObject++;
                        });
                        break;
                    }
                    case 1: //COFF
                    {
                        MessageBox.Show("Compiling COFF Object Files Has Not Been Implemented Please Refrain For Using Until Further Notice!", "Unavailable Feature In BETA", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        break;
                    }
                }
                GC.Collect();
            }
        }

        private void extraParameters_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == Keys.F || e.KeyCode == Keys.L || e.KeyCode == Keys.O) && !Control.IsKeyLocked(Keys.CapsLock))
            {
               
                e.SuppressKeyPress = true; 
            }
        }
    }
}
